package com.caocaog.market.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.caocaog.market.entity.ShopFareConfig;
import com.caocaog.market.enums.ShopFareConfigEnum;
import com.caocaog.market.mapper.ShopFareConfigMapper;
import com.caocaog.market.service.ShopFareConfigService;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.exception.CheckFailedException;
import org.jeecg.common.exception.JeecgBootException;
import org.springframework.aop.framework.AopContext;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.math.BigDecimal;
import java.util.Objects;
import java.util.Optional;

import static org.jeecg.common.constant.CacheConstant.SHOP_FARE_CONFIG;

/**
 * 店铺加价配置
 *
 * @author chechen
 * @date 2022-10-29
 */
@Service
@EnableAspectJAutoProxy(exposeProxy = true)
@CacheConfig(cacheNames = SHOP_FARE_CONFIG)
public class ShopFareConfigServiceImpl extends ServiceImpl<ShopFareConfigMapper, ShopFareConfig> implements ShopFareConfigService {

    @Override
    @Cacheable(key = "#shopId")
    public ShopFareConfig findShopFareConfigByShopId(String shopId) {
        return baseMapper.findShopFareConfigByShopId(shopId);
    }


    @Override
    @CacheEvict(key = "#shopFareConfig.shopId")
    public int addShopFareConfig(ShopFareConfig shopFareConfig) {
        ShopFareConfig shopFareConfigByShopId = ((ShopFareConfigServiceImpl) AopContext.currentProxy()).findShopFareConfigByShopId(shopFareConfig.getShopId());
        if (Objects.nonNull(shopFareConfigByShopId)) {
            throw new CheckFailedException("店铺已有加价配置，请勿重复添加");
        }

        return baseMapper.insert(verification(shopFareConfig));
    }


    @Override
    @CacheEvict(key = "#shopFareConfig.shopId")
    public int updateShopFareConfig(ShopFareConfig shopFareConfig) {
        return baseMapper.updateById(verification(shopFareConfig));
    }

    @Override
    @CacheEvict(key = "#shopId")
    public int changeShopFareConfigState(String shopId, ShopFareConfigEnum shopFareConfigEnum) {
        ShopFareConfig shopFareConfig = Optional.ofNullable(((ShopFareConfigServiceImpl) AopContext.currentProxy()).findShopFareConfigByShopId(shopId))
                .orElseThrow(() -> new CheckFailedException("没有找到相应的店铺加价配置, shopId-> " + shopId));

        switch (shopFareConfigEnum) {
            case NIGHTLY_FARE:
                shopFareConfig.setIsOpenNightlyFare(shopFareConfig.getIsOpenNightlyFare() == 0 ? 1 : 0);
                break;
            case WEATHER_FARE:
                shopFareConfig.setIsOpenSpecialWeatherFare(shopFareConfig.getIsOpenSpecialWeatherFare() == 0 ? 1 : 0);
                break;
            default:
                throw new JeecgBootException("未找到相应店铺加价配置类型");
        }

        return updateShopFareConfig(shopFareConfig);
    }


    /**
     * 参数校验
     *
     * @param shopFareConfig 店铺加价配置
     */
    private static ShopFareConfig verification(ShopFareConfig shopFareConfig) {

        if (shopFareConfig.getIsOpenNightlyFare() == 1) { // 开启夜间加价
            Assert.isTrue(Objects.nonNull(shopFareConfig.getNightlyFares()) // 夜间加价配置不为null
                            && !shopFareConfig.getNightlyFares().isEmpty() // 夜间加价配置list长度不为0
                    , "夜间加价配置不能为空");

            shopFareConfig.getNightlyFares().forEach(nightlyFare ->
                    Assert.isTrue(StringUtils.isNotBlank(nightlyFare.getStartTime()) // 夜间加价开始时间不为null
                            && StringUtils.isNotBlank(nightlyFare.getEndTime()) // 夜间加价结束时间不为null
                            && Objects.nonNull(nightlyFare.getAmount()) // 夜间加价金额不为null
                            && nightlyFare.getAmount().compareTo(BigDecimal.ZERO) > 0 // 夜间加价金额大于0
                    , "夜间加价配置参数不合法"));

        }


        if (shopFareConfig.getIsOpenSpecialWeatherFare() == 1) // 开启特殊天气加价
            Assert.isTrue(Objects.nonNull(shopFareConfig.getSpecialWeatherFare()) // 特殊天气加价金额不为null
                            && shopFareConfig.getSpecialWeatherFare().compareTo(BigDecimal.ZERO) > 0 // 特殊天气加价金额大于0
                    , "特殊天气加价金额不正确");

        return shopFareConfig;
    }

}
